<?php
/**
 * Created by PhpStorm.
 * 商家注册/登陆
 * User: caicm
 * Date: 2017/6/14
 * Time: 16:41
 */

namespace WMApi\Controller;

use WMApi\Model\UsersModel;
use WMApi\Model\WmSellerModel;
use WMApi\Model\WmStoreModel;
use WMApi\Service\SellerService;

class SellerRegController extends SellerBaseController  {

    /**
     *发送验证码
     * Created By caicm
     * @param int phone 手机号
     * @param int type 验证码类型 1:商家注册，2:找回密码
     */
    public function send_verificode(){
        $user_phone = I("post.phone");
        $type = I('post.type', 0, 'int');
        if(empty($type)){
            returnApiError("type is null");
        }
        if(empty($user_phone)){
            returnApiError("手机号码不能为空");
        }
        if(!is_mobile($user_phone)){
            returnApiError("请输入正确的手机号");
        }

        $users_model = new UsersModel();
        $users_num = $users_model->get_users_conut_by_phone($user_phone);
        if($type == 1 && $users_num>0){
            returnApiError('该手机已被注册');
        }elseif($type == 2 && $users_num==0){
            returnApiError('该手机未注册');
        }

        $ver_code = getSmsCode();
        $send_result = A("SDXApi/Smsapi")->send_message_api("ver_code", $user_phone, 0, 0, 0, $ver_code, 0);
        if($send_result){
            S("seller_".$user_phone."_code",$ver_code,600);    //验证码保持600秒
            returnApiSuccess(null,'发送验证码成功');
        }else{
            returnApiError('发送验证码失败');
        }

    }


    /**
     * 商家登陆
     * Created By caicm
     * @param int phone 手机号
     * @param string password 密码
     */
    public function login(){
        $password = I('post.password');
        $user_phone = I("post.phone");
        if(empty($user_phone)){
            returnApiError("手机号码不能为空");
        }
        if(!is_mobile($user_phone)){
            returnApiError("请输入正确的手机号");
        }
        if(empty($password)){
            returnApiError("密码不能为空");
        }

        $users_model = new UsersModel();
        $seller_model = new WmSellerModel();
        $store_model = new WmStoreModel();

        $seller_info = $users_model->find_users_seller_info(0, 0, $user_phone);
        if(!$seller_info){
            returnApiError("该手机未注册！");
        }

        //密码加密
        $user_password = compile_password(array('password'=>$password, 'ec_salt'=>$seller_info['ec_salt']));
        if($seller_info['password'] !=$user_password){
            returnApiError("手机或密码有误！");
        }

        //商家默认店铺id
        $default_store_id = $store_model->get_default_store_id($seller_info['seller_id']);

        $data = array(
            'seller_id' => $seller_info['seller_id'],
            'store_id' => $default_store_id,
            'token' => $seller_info['seller_token']
        );
        S('seller_token_'.$seller_info['seller_id'], $seller_info['seller_token'], 86400); //缓存时间1天
        returnApiSuccess($data, '登陆成功！');

        /**
         * 下面是每次登陆修改商家token(目前先不用)
         */
        /**
        //获取签名加密
        $seller_token = $this->produce_string();
        //修改商家签名
        $update_token = $seller_model->update_seller_token($seller_info['seller_id'], $seller_token);
        if($update_token){
            //商家默认店铺id
            $default_store_id = $store_model->get_default_store_id($seller_info['seller_id']);

            $data = array(
                'seller_id' => $seller_info['seller_id'],
                'store_id' => $default_store_id,
                'token' => $seller_token
            );
            S('seller_token_'.$seller_info['seller_id'], $seller_token, 86400); //缓存时间1天
            returnApiSuccess($data, '登陆成功！');
        }else{
            returnApiError('登陆失败！');
        }
         */

    }


    /**
     * 商家注册
     * Created By caicm
     * @param int phone 手机号
     * @param string password 密码
     * @param int verifi_code 验证码
     */
    public function register(){

        $password = I('post.password');
        $user_phone = I("post.phone");
        $verifi_code = I('post.verifi_code');
        if(empty($user_phone)){
            returnApiError("手机号码不能为空");
        }
        if(!is_mobile($user_phone)){
            returnApiError("请输入正确的手机号");
        }
        if(empty($password)){
            returnApiError("密码不能为空");
        }
        if(empty($verifi_code)){
            returnApiError("验证码不能为空");
        }
        if(S("seller_".$user_phone."_code") != $verifi_code){
            returnApiError("验证码不正确");
        }

        $users_model = new UsersModel();
        $seller_service = new SellerService();

        if($users_model->get_users_conut_by_phone($user_phone)){
            returnApiError('该手机已被注册');
        }

        //新用户--自动注册账号
        $username = 'wx_' . gmtime().mt_rand(1, 99);
        $email = $username."@shidexian.com";

        //获取签名加密
        $seller_token = $this->produce_string();

        //密码加密key
        $ec_salt = get_random_str(6);
        //密码加密
        $password = compile_password(array('password'=>$password, 'ec_salt'=>$ec_salt));

        //注册商家
        $seller_id = $seller_service->reg_seller($username, $password, $email, $ec_salt, $user_phone, $seller_token);

        $data = array(
            'seller_id' => $seller_id,
            'token' => $seller_token
        );

        if($seller_id){
            //清空验证码
            S("seller_".$user_phone."_code",null);
            returnApiSuccess($data, '注册成功！');
        }else{
            returnApiError('注册失败！');
        }

    }

    /**
     * 找回密码
     * Created By caicm
     * @param int phone 手机号
     * @param string password 密码
     * @param int verifi_code 验证码
     */
    public function retrieve_password(){
        $password = I('post.password');
        $user_phone = I("post.phone");
        $verifi_code = I('post.verifi_code');
        if(empty($user_phone)){
            returnApiError("手机号码不能为空");
        }
        if(!is_mobile($user_phone)){
            returnApiError("请输入正确的手机号");
        }
        if(empty($password)){
            returnApiError("密码不能为空");
        }
        if(empty($verifi_code)){
            returnApiError("验证码不能为空");
        }
        if(S("seller_".$user_phone."_code") != $verifi_code){
            returnApiError("验证码不正确");
        }

        $users_model = new UsersModel();

        if(!$users_model->get_users_conut_by_phone($user_phone)){
            returnApiError('该手机未注册');
        }

        //密码加密key
        $ec_salt = get_random_str(6);
        //密码加密
        $password = compile_password(array('password'=>$password, 'ec_salt'=>$ec_salt));

        $res = $users_model->update_password($user_phone, $password, $ec_salt);
        if($res){
            //清空验证码
            S("seller_".$user_phone."_code",null);
            returnApiSuccess(null, '修改成功！');
        }else{
            returnApiError('修改失败！');
        }

    }


    /**
     * 生成随机码
     * @return mixed
     */
    private function produce_string(){
        $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        $chars   =   str_shuffle($chars);
        $str_name     =   substr($chars,0,20);
        $str_vaule=md5($str_name.time());
        return $str_vaule;
    }
}